import { getAll } from '@/api/label'

const state = {
    labels: []
}

const mutations = {
    INIT_LABELS: (state, labels) => {
        state.labels = labels
    }
}

const actions = {
    // user login
    init({ commit }) {
        return new Promise((resolve, reject) => {
            getAll().then(response => {
                commit('INIT_LABELS', response.data)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },
    // 通过id 获取标签
    getById(id) {
        let res = {}
        state.labels.forEach(item => {
            if (item.parentId === 0) {
                item.children.forEach(item => {
                    if (item.id === id) {
                        res = item
                    }
                })
            } else {
                if (item.id === id) {
                    res = item
                }
            }
        })
        return res
    },


}

export default {
    namespaced: true,
    actions,
    state,
    mutations
}